import { objectAny } from "../../options/base";

export function cameraSelectImage(
    selectCount:number|undefined = 1,
    compression:boolean|undefined = true
):Promise<{
    code:0|1;
    err?:objectAny;
    data?:{
        list:Array<string>;
        compression:boolean   
    }
}>{
    return new Promise(resolve=>{
        uni.chooseImage({
            count: selectCount,
            sizeType: ['compressed'], 
            sourceType: ['camera'], 
            success:  (res)=> {
                if(!compression){
                    return resolve({
                        code:0,
                        data:{
                            //@ts-ignore
                            list:''.split.call(res.tempFilePaths,','),
                            compression
                        }
                    })
                }
                uni.compressImage({
                    src: res.tempFilePaths[0],
                    quality:40,
                    success:  ({tempFilePath}:{tempFilePath:string})=> {
                        return resolve({
                            code:0,
                            data:{
                                //@ts-ignore
                                list:''.split.call(tempFilePath,','),
                                compression
                            }
                        })
                    },
                    fail:(err:objectAny)=>{
                        console.log(err)
                        return resolve({
                            code:0,
                            data:{
                                //@ts-ignore
                                list:''.split.call(res.tempFilePaths,','),
                                compression
                            }
                        })
                    }
                });
            },
            fail:(err:objectAny)=>{
                if(err.errMsg===`chooseImage:fail cancel`){
                    console.error(`取消选择图片`)
                }
                return resolve({
                    code:1,
                    err
                })
            }
        });
    })
}